<template>
  <div class="holiday-approval">
    <!-- <van-nav-bar
      title="请假审批"
      left-arrow
      :safe-area-inset-top="true"
      @click-left="onClickLeft"
    /> -->
    <div class="holiday-approval-body" v-if="holidayList.length === 0">
      <van-pull-refresh
        style="height: 100%"
        v-model="refreshLoading"
        success-text="刷新成功！"
        @refresh="onRefresh"
      >
        <van-empty image-size="12rem" description="暂无请假" />
      </van-pull-refresh>
    </div>
    <div class="holiday-approval-body" v-else>
      <van-pull-refresh
        style="height: 100%"
        v-model="refreshLoading"
        success-text="刷新成功！"
        @refresh="onRefresh"
      >
        <div class="item" v-for="item in holidayList" :key="item.id">
          <div class="content">
            <div>
              <span class="label">申请人：</span>
              <span>{{ item.userName }}</span>
            </div>
            <div>
              <span class="label">理由：</span>
              <span>{{ item.reason }}</span>
            </div>
            
          </div>
          <div class="operation">
            <span class="refused" @click="goApproval(item, 2)">打回</span>
            <span class="through" @click="goApproval(item, 1)">通过</span>
          </div>
        </div>
      </van-pull-refresh>
    </div>
  </div>
</template>

<script>
import { ref, onMounted, toRefs } from 'vue'
import { getCmsLeave, updateCmsLeaveById } from '@/api/meeting/holiday.js'
import { Notify } from 'vant'
export default {
  name: 'HolidayApproval',
  props: {
    meetingId: {
      type: String,
      required: true,
      default: ""
    }
  },
  setup(props) {
    // const route = useRoute()

    // const meetingId = route.query.meetingId

    const { meetingId } = toRefs(props)

    onMounted(() => {
      getApplyHolidayList()
    })

    let refreshLoading = ref(false)

    // 刷新
    function onRefresh() {
      getApplyHolidayList()
    }

    let holidayList = ref([])


    // 获取请假列表
    async function getApplyHolidayList() {
      const res = await getCmsLeave({ meetingId: meetingId.value, status: 0 })
      console.log(res)

      refreshLoading.value = false

      if (res.data.code === 400) {
        Notify({ type: 'error', duration: 1000, message: res.data.message })
        return
      }
      holidayList.value = res.data.data.records
    }

    // 审批请假
    async function goApproval(item, value) {
      const res = await updateCmsLeaveById({
        id: item.id,
        meetingId: meetingId.value,
        status: value,
      })
      if (res.data.code === 400) {
        Notify({ type: 'error', duration: 1000, message: res.data.message })
        return
      }
      Notify({
        type: 'success',
        duration: 2000,
        message: `已${value === 1 ? '通过' : '打回'}${item.userName}请假`,
      })
      getApplyHolidayList()
    }

    // const onClickLeft = () => history.back()

    return {
      holidayList,
      refreshLoading,
      // onClickLeft,
      goApproval,
      onRefresh,
    }
  },
}
</script>

<style lang="scss" scoped>
.holiday-approval {
  width: 100vw;
  height: 100vh;
  background-color: var(--global-bg);
  .holiday-approval-body {
    width: 100%;
    height: calc(100vh - var(--van-nav-bar-height));
    overflow: auto;
    padding: 1rem;
    box-sizing: border-box;
    .item {
      width: 100%;
      height: 10vh;
      margin-bottom: 1rem;
      border-radius: 1rem;
      background-color: #fff;
      box-sizing: border-box;
      padding: 1.5rem 1.5rem;
      display: flex;
    }

    .content {
      flex: 1;
      height: 100%;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      font-size: 1.4rem;

      .label {
        font-weight: bold;
      }
    }
    .operation {
      height: 100%;
      width: 15rem;
      display: flex;
      justify-content: right;
      align-items: center;

      span {
        margin-right: 1rem;
        width: 6rem;
        height: 2.8rem;
        line-height: 2.8rem;
        text-align: center;
        font-size: 1.3rem;
        border-radius: 0.5rem;
      }
      .through {
        background-color: var(--success-btn-bg);
        color: var(--success-btn-color);
      }
      .refused {
        background-color: var(--refused-btn-bg);
        color: var(--refused-btn-color);
      }
    }
  }
}
</style>